/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "abort.h"
#include "data.h"
#include <cstdio>
#include <cstdlib>

#if defined(TARGET_IPHONE)
#include "platform/iOS/data_impl.h"
#elif defined(TARGET_WINDOWS)
#include "platform/windows/data_impl.h"
#endif

#if GRAPH2D_LOG_DETAIL_LEVEL >= 1
#include <libmana.h>
#endif

namespace Graph2D
{
	Data::Data() : instance(NULL)
	{
	}

	Data::Data(const void* buffer, const size_t size)
	{
		instance = Implementation::Data::create(buffer, size);
#if GRAPH2D_LOG_DETAIL_LEVEL >= 1
		MANA_TRACE("Data::load: %p %dbyte(s) #%p ... %s\n", buffer, size, instance, getBuffer() ? "OK" : "NG");
#endif
	}

	Data::Data(const char* filename, const bool resource)
	{
		instance = Implementation::Data::create(std::string(filename), resource);
#if GRAPH2D_LOG_DETAIL_LEVEL >= 1
		MANA_TRACE("Data::load: %s %dbyte(s) #%p ... %s\n", filename, getSize(), instance, getBuffer() ? "OK" : "NG");
#endif
	}

	Data::Data(const std::string& filename, const bool resource)
	{
		instance = Implementation::Data::create(filename, resource);
#if GRAPH2D_LOG_DETAIL_LEVEL >= 1
		MANA_TRACE("Data::load: %s %dbyte(s) #%p ... %s\n", filename.c_str(), getSize(), instance, getBuffer() ? "OK" : "NG");
#endif
	}

	Data::~Data()
	{
		if(instance)
		{
#if GRAPH2D_LOG_DETAIL_LEVEL >= 1
			MANA_TRACE("Data::unload: #%p ... OK\n", instance);
#endif
			Implementation::Data::release(instance);
		}
	}

	const void* Data::getBuffer() const
	{
		return Implementation::Data::getBuffer(instance);
	}

	size_t Data::getSize() const
	{
		return Implementation::Data::getSize(instance);
	}

	bool Data::valid() const
	{
		return getBuffer() && getSize() > 0;
	}

	bool Data::write(const std::string& filename)
	{
		return Implementation::Data::write(instance, filename);
#if 0
//abort(ERROR_CODE_FILE_WRITE_FAILED, filename.c_str());
		/*
#if __STDC_WANT_SECURE_LIB__
		FILE* file;
		if(fopen_s(&file, filename.c_str(), "wb") != 0)
			abort(ERROR_CODE_FILE_OPEN_FAILED, filename.c_str());
#else
		FILE* file = fopen(filename.c_str(), "wb");
		if(file == NULL)
			abort(ERROR_CODE_FILE_OPEN_FAILED, filename.c_str());
#endif
		if(fwrite(buffer, 1, length, file) != length)
		{
			fclose(file);
			abort(ERROR_CODE_FILE_WRITE_FAILED, filename.c_str());
		}

		fclose(file);
		*/

		return true;
#endif
	}

	const char* Data::getDocumentPath()
	{
		return Implementation::Data::getDocumentPath();
	}

	const char* Data::getResoucePath()
	{
		return Implementation::Data::getResoucePath();
	}

	bool Data::exist(const std::string& filename, const bool resource)
	{
		return Implementation::Data::exist(filename, resource);
	}
}
